# http://wiki.openqa.org/display/WTR/HTML+report+class

class XMLReporter
  # Initialize the report class


  def initialize()
    @overallResult = 'PASSED'
    @reportContent1 = ''
    @reportContent2 = ''
    @tcNbr = 1
  end

  # Create a report
  def createReport(reportName)
    # Get current time
    t = Time.now
    strTime = t.strftime("_%Y%m%d_%H%M%S.xml")
    strNiceTime = t.strftime("%m/%d/%Y @ %I:%M:%S %p")
    @strYear = t.year.to_s

   
    # Create the report name
    strTotalReport = reportName + strTime
    strTestCaseName = File.basename(reportName)
    strReportNameShort = File.basename(strTotalReport)
    # Create the XML report
    strFile = File.open(strTotalReport, 'w')

    #Get the username
    case $hostOS
    when "mswin32" then @username = ENV['UserName']
    when /linux/   then  @username = ENV['LOGNAME']
    when /darwin/  then @username = ENV['LOGNAME']
    else @username = "#{@hostOS} not handled."
    end
      

    # Format the header of the XML report
    @reportContent1  = "<TestRun>\n"
    @reportContent1 += " <TestCaseName>" + strTestCaseName + "</TestCaseName>\n"
    @reportContent1 += " <ExecutedBy>" + @username + "</ExecutedBy>\n"
    @reportContent1 += " <Result>"

    @reportContent2 = "</Result>\n"
    @reportContent2 += " <Details>\n"

    # Close the report
    strFile.close

    return strTotalReport
  end

  def addtoReport(reportName, command, result, parameters, expected)
    # Format the body of the XML report
    case result
    when "PASSED"
      @reportContent2 += "  <Action>\n"
      @reportContent2 += "   <command>" + command + "</command>\n"
      @reportContent2 += "   <status>" + result + "</status>\n"
      @reportContent2 += "   <parameters>" + parameters + "</parameters>\n"
      @reportContent2 += "   <expected>" + expected + "</expected>\n"
      @reportContent2 += "  </Action>\n"
    when "FAILED"
      @overallResult = "FAILED"
      @reportContent2 += "  <Action>\n"
      @reportContent2 += "   <command>" + command + "</command>\n"
      @reportContent2 += "   <status>" + result + "</status>\n"
      @reportContent2 += "   <parameters>" + parameters + "</parameters>\n"
      @reportContent2 += "   <expected>" + expected + "</expected>\n"
      @reportContent2 += "  </Action>\n"
    when "HEADING"
    else
      @reportContent2 += "  <Action>\n"
      @reportContent2 += "   <command>" + command + "</command>\n"
      @reportContent2 += "   <status>" + result + "</status>\n"
      @reportContent2 += "   <parameters>" + parameters + "</parameters>\n"
      @reportContent2 += "   <expected>" + expected + "</expected>\n"
      @reportContent2 += "  </Action>\n"
    end
    @tcNbr += 1
   
  end

  def finishReport(reportName)
    # Open the XML report
    strFile = File.open(reportName, 'a')

    # Format the footer of the XML report
    @reportContent2 += " </Details>\n"
    @reportContent2 += "</TestRun>\n"

    strFile.puts(@reportContent1 + @overallResult + @reportContent2)

    # Close the report
    strFile.close
  end

end
